package huoye.shops.base;

import android.Manifest;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.codeideology.android.AXIActivity;
import com.codeideology.android.http.okhttp.OkHttpUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Optional;
import commonality.analytics.AXIAnalyticsSDK;
import huoye.shops.R;
import pub.devrel.easypermissions.EasyPermissions;

import static huoye.shops.base.BaseFragment.TYPE_START_MANAGE;
import static okhttp3.internal.Internal.instance;


/**
 * Created by user on 2017/7/24.
 */

public abstract class BaseActivity extends AXIActivity implements Toolbar.OnMenuItemClickListener{


    @Nullable
    @BindView(R.id.toolbar_title_tv)
    public TextView tvTitle;

    @Nullable
    @BindView(R.id.toolbar_bg)
    public RelativeLayout tbBg;

    @Nullable
    @BindView(R.id.toolbar_right_btn)
    public Button btnRight;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setTitle("");
        highApiEffects();
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    private void highApiEffects() {
        getWindow().getDecorView().setFitsSystemWindows(true);
        //透明状态栏 @顶部
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏 @底部    这一句不要加，目的是防止沉浸式状态栏和部分底部自带虚拟按键的手机（比如华为）发生冲突，注释掉就好了
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }


    protected String getIntentId() {
        Intent intent = this.getIntent();
        if (intent != null) {
            return intent.getStringExtra(EXTRAS_KEY_ID);
        }
        return null;
    }




    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home: {
                onBackClick();
            }
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home: {
                onBackClick();
            }
        }
        return true;
    }

    protected void onTopLeftBtnClick() {
        onBackClick();
    }


    protected void onBackClick() {
        finish();
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }

    public void onResume() {
        super.onResume();
        AXIAnalyticsSDK.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        AXIAnalyticsSDK.onPause(this);
    }

    @Override
    public void setTitle(CharSequence title) {
        if (tvTitle != null) {
            tvTitle.setText(title);
        } else {
            super.setTitle(title);
        }

    }



    public void setRightTv(CharSequence title) {
        if (btnRight != null) {
            btnRight.setText(title);
        } else {
            super.setTitle(title);
        }

    }

    @Override
    public void setTitle(int titleId) {
        super.setTitle(titleId);
        if (tvTitle != null) {
            tvTitle.setText(titleId);
        } else {
            super.setTitle(titleId);
        }
    }

    @OnClick(R.id.toolbar_left_iv)
    @Optional
    public void topLeftIvClick() {
        onTopLeftIvClick();
    }

    public void onTopLeftIvClick() {
            finish();
    }


    @OnClick(R.id.toolbar_right_btn)
    @Optional
    public void topRightIvClick() {
        onTopRightIvClick();
    }

    public void onTopRightIvClick() {

    }

    public void  hideRightBtn(){
        btnRight.setVisibility(View.GONE);
    }


    public void  showRightBtn(){
        btnRight.setVisibility(View.VISIBLE);
    }


    //设置Toobar背景颜色
    protected void setToolbarImgColor() {
        tbBg.setBackgroundColor(getResources().getColor(R.color.shallow_grey));
    }

    public void startActivityAndFinish(Class<? extends Activity> aClass, Bundle bundle, boolean b) {

    }

    /**
     * 通用界面跳转
     *
     * @param context
     * @param activty
     */
    public static void startActivityData(Context context, Class activty, String type) {
        Intent intentData = new Intent(context, activty);
        intentData.putExtra(TYPE_START_MANAGE,type);
        context.startActivity(intentData);
    }

    public void onNetworkFail() {
        this.makeToast("网络好像有点问题，请检查后重试");
    }




}

